package com.pronhub.luowowo.website.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.pronhub.luowowo.article.query.TravelQuery;
import com.pronhub.luowowo.article.service.IStrategyCommendService;
import com.pronhub.luowowo.article.service.ITravelCommendService;
import com.pronhub.luowowo.article.service.ITravelService;
import com.pronhub.luowowo.member.domain.UserInfo;
import com.pronhub.luowowo.search.domain.DestinationTemplate;
import com.pronhub.luowowo.search.domain.StrategyTemplate;
import com.pronhub.luowowo.search.domain.TravelTemplate;
import com.pronhub.luowowo.search.domain.UserInfoTemplate;
import com.pronhub.luowowo.search.query.SearchQueryObject;
import com.pronhub.luowowo.search.service.*;
import com.pronhub.luowowo.search.vo.SearchResultVO;
import com.pronhub.luowowo.service.IUserInfoRedisService;
import com.pronhub.luowowo.website.web.util.CookieUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.naming.directory.SearchResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService infoRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    //目的地的ES
    @Reference
    private IDestinationSearchService destinationSearchService;
    //攻略
    @Reference
    private IStrategySearcheService strategySearcheService;
    //用户
    @Reference
    private IUserInfoSearcheService userInfoSearcheService;
    //查游记
    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private ISearchService searchService;

    /**
     * 检查是否登录 然后延长时间的
     * @param response  响应
     * @param request   请求
     * @return
     */
    @RequestMapping("") //第一次登陆和访问首页的时候进行更新时间
    public String index(Model model , HttpServletResponse response, HttpServletRequest request){
        String token = CookieUtil.getToken(request, response);
        if (token!=null){
            UserInfo userInfo = infoRedisService.getUserInfoByToken(token);
            request.getSession().setAttribute("userInfo",userInfo);
        }


        //首页游记推荐
        //tcs
        model.addAttribute("tcs", travelCommendService.getTop5());
        //攻略推荐
        //scs
        model.addAttribute("sc", strategyCommendService.getTop5().get(0));

        return "index/index";
    }

    @RequestMapping("index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "index/travelPageTpl";
        //404  找不到页面也算是404
    }

    @RequestMapping("q")
    public String q(Model model,@ModelAttribute("qo") SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDest(model,qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model,qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model,qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model,qo);
            default:return searchAll(model,qo);
        }
    }
    //攻略
    private String searchStrategy(Model model,SearchQueryObject qo){

        return "index/searchStrategy";
    }
    //目的地
    private String searchDest(Model model,SearchQueryObject qo){
        SearchResultVO data = new SearchResultVO();
        //1 查询目的地是否的存在
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());

        if (dest!=null){
            //2存在 查询约定的前5篇 攻略 游记 用户
            List<StrategyTemplate> sts = strategySearcheService.getByDestName(qo.getKeyword());
            if (sts.size()>5){
                sts = sts.subList(0,5);
            }
            List<TravelTemplate> ts = travelSearchService.findByDestName(qo.getKeyword());
            if (ts.size()>5){
                ts = ts.subList(0,5);
            }
            List<UserInfoTemplate> us = userInfoSearcheService.getByDestName(qo.getKeyword());
            if (us.size()>5){
                us = us.subList(0,5);
            }
            data.setUsers(us);
            data.setStrategys(sts);
            data.setTravels(ts);
            model.addAttribute("data",data);
        }
        //不存在 提示不存在
        model.addAttribute("dest", dest);
        return "index/searchDest";
    }
    //游记
    private String searchTravel(Model model,SearchQueryObject qo){
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.INDEX_NAME, TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("ts",ts);
        return "index/searchTravel";
    }
    //用户名字
    private String searchUser(Model model,SearchQueryObject qo){
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("us",us);
        return "index/searchUser";
    }
    //全部
    private String searchAll(Model model,SearchQueryObject qo){
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME,
                TravelTemplate.INDEX_NAME, TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("ts",ts);
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME,
                UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("us",us);
        Page<StrategyTemplate> sts = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME,
                StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("sts",sts);
        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME,
                DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        model.addAttribute("ds",ds);


        SearchResultVO data = new SearchResultVO();

        data.setTotal(ds.getTotalElements()+us.getTotalElements()
            +sts.getTotalElements()+ts.getTotalElements());

        data.setTravels(ts.getContent());
        data.setStrategys(sts.getContent());
        data.setUsers(us.getContent());
        data.setDests(ds.getContent());

        model.addAttribute("data", data);
        return "index/searchAll";
    }
}
